#include "wheelbox.h"
#include <qwt_math.h>
#include <qpen.h>
#include <qwt_knob.h>
#include <qwt_round_scale_draw.h>
#include <qwt_scale_engine.h>
#include <qlabel.h>
#include <qevent.h>
#include <qwt_wheel.h>
#include <qlcdnumber.h>
#include <qlayout.h>
#include <qevent.h>
#include <qapplication.h>

class Wheel : public QwtWheel
{
public:
	Wheel(WheelBox *parent) :
		QwtWheel(parent)
	{
		setFocusPolicy(Qt::WheelFocus);
	}

	virtual bool eventFilter(QObject *object, QEvent *event)
	{
		if (event->type() == QEvent::Wheel)
		{
			const QWheelEvent *we = static_cast<QWheelEvent *>(event);

			QWheelEvent wheelEvent(QPoint(5, 5), we->delta(),
				we->buttons(), we->modifiers(),
				we->orientation());

			QApplication::sendEvent(this, &wheelEvent);
			return true;
		}
		return QwtWheel::eventFilter(object, event);
	}
};

WheelBox::WheelBox(const QString &title,
	double min, double max, double stepSize, QWidget *parent) :
	QWidget(parent)
{

	d_number = new QLCDNumber(this);
	d_number->setSegmentStyle(QLCDNumber::Filled);
	d_number->setAutoFillBackground(true);
	d_number->setFixedHeight(40);
	d_number->setFocusPolicy(Qt::WheelFocus);

	QPalette pal(Qt::black);
	pal.setColor(QPalette::WindowText, Qt::green);
	d_number->setPalette(pal);

	d_wheel = new Wheel(this);
	d_wheel->setOrientation(Qt::Horizontal);
	d_wheel->setInverted(false);
	d_wheel->setRange(min, max);
	d_wheel->setSingleStep(stepSize);
	d_wheel->setPageStepCount(5);
	d_wheel->setFixedHeight(25);

	d_number->setFocusProxy(d_wheel);

	QFont font("\345\256\213\344\275\223", 9);

	d_label = new QLabel(title, this);
	d_label->setFont(font);

	QVBoxLayout *vLayout = new QVBoxLayout(this);
	vLayout->setSpacing(5);
	vLayout->setContentsMargins(0, 0, 0, 0);
	vLayout->addWidget(d_label, 0, Qt::AlignTop);
	vLayout->addWidget(d_number, 10);
	vLayout->addWidget(d_wheel);



	connect(d_wheel, SIGNAL(valueChanged(double)),
		d_number, SLOT(display(double)));
	connect(d_wheel, SIGNAL(valueChanged(double)),
		this, SIGNAL(valueChanged(double)));
}

void WheelBox::setTheme(const QColor &color)
{
	d_wheel->setPalette(color);
}

QColor WheelBox::theme() const
{
	return d_wheel->palette().color(QPalette::Window);
}

void WheelBox::setValue(double value)
{
	d_wheel->setValue(value);
	d_number->display(value);
}

double WheelBox::value() const
{
	return d_wheel->value();
}

